package io.piped;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;


public class PipedExample {


    public static void main(String[] args) throws IOException {
        PipedOutputStream output = new PipedOutputStream();
        PipedInputStream input = new PipedInputStream(output);


        new Thread(
                ()->{
                    try {
                        output.write("Hello World!".getBytes());

                    } catch (Exception e) {
                        e.printStackTrace();

                    }finally {
                        try {
                            output.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
        ).start();

        new Thread(() -> {
            int i;
            try {
                while ((i = input.read()) != -1) {

                    System.out.print((char)i);


                }

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }
}
